# ICMPで確認
use strict;
use warnings;
use Net::Ping;

my $g_return = ping_main( @ARGV );

1;

sub pinger{
	my ( $host, $timeout, $interval, $target, $trymax, $log ) = @_;
	my $border = 0;

	my $p = Net::Ping->new("icmp");
	foreach my $try ( 1 .. $trymax){
		$border += active_sonar( $p, $host, $timeout );
		print "SUCCESS / TRY / TRY_MAX : $border / $try / $trymax\n" if( $log eq 'ON' );
		sleep( $interval / 1000 );
	}
	if( $border >= $target ){
		print "BORDER COUNT $target CLEAR!!\n" if( $log eq 'ON' );
		return 1;
	}
	print "BORDER COUNT $target NOT CLEAR...\n" if( $log eq 'ON' );
	return 0;
}

sub active_sonar{
	my ( $p, $host, $timeout ) = @_;
	if ( $p->ping( $host, $timeout / 1000 ) ) {
	    return 1;
	} else {
		return 0;
	}
}


sub ping_main{
	my @args = @_;
	my $h_parameter = parse_params( @args );
	my $result = pinger($h_parameter->{'-host'       }
	                   ,$h_parameter->{'-to'         }
	                   ,$h_parameter->{'-i'          }
	                   ,$h_parameter->{'-rate_border'}
	                   ,$h_parameter->{'-rate_try'   }
	                   ,$h_parameter->{'-log'        }
	             );
	return $result;
}


sub parse_params{
	my @args = @_;
	my ( $key, $value );
	my $h_parameter;
	foreach my $arg ( @args ){
		( $key, $value ) = split( /=/, $arg );
		display_help() if( !defined $value );
		$h_parameter->{$key} = $value;
	}
	if( !defined $h_parameter->{'-to'} ){
		$h_parameter->{'-to'} = 3000;
	}
	if( !defined $h_parameter->{'-i'} ){
		$h_parameter->{'-i'} = 1000;
	}
	if( !defined $h_parameter->{'-log'} ){
		$h_parameter->{'-log'} = 'OFF';
	}
	if(   !defined $h_parameter->{'-rate'}
	   || !defined $h_parameter->{'-host'}
	   || $h_parameter->{'-to'} !~ /^\d+$/
	   || $h_parameter->{'-i' } !~ /^\d+$/ )
	{
		display_help();
	}
	if(   $h_parameter->{'-log'} ne 'ON'
	   && $h_parameter->{'-log'} ne 'OFF' )
	{
		display_help();
	}

	( $h_parameter->{'-rate_border'}, $h_parameter->{'-rate_try'} ) = rate_check( split( /\//, $h_parameter->{'-rate'} ) );
	return $h_parameter;
}


sub rate_check{
	my ( $border, $try ) = @_;
	if(   defined $border
	   && defined $try
	   && $border =~ /^\d+$/
	   && $try    =~ /^\d+$/
	   && int($border) <= int($try) )
	{
		return ( $border, $try );
	}
	display_help();
}


sub display_help{
	print "+--------------------------------------------+\n";
	print "|   ping.pl                                  |\n";
	print "+--------------------------------------------+\n";
	print "|   1.HOW TO USE                             |\n";
	print "|      perl -w ping.pl -host=HOST            |\n";
	print "|       -rate=INT1/INT2 [-i=SEC]             |\n";
	print "|       [-to=SEC] [-log=SWITCH]              |\n";
	print "|                                            |\n";
	print "|   2.ABOUT PARAMETERS                       |\n";
	print "|      -host : host address                  |\n";
	print "|      -rate : success count / try count     |\n";
	print "|      -i    : interval of ping.             |\n";
	print "|              default : 1000                |\n";
	print "|              unit    : mili second.        |\n";
	print "|      -to   : timeout of ping response.     |\n";
	print "|              default : 3000                |\n";
	print "|              unit    : mili second.        |\n";
	print "|      -log  : log mode switch.              |\n";
	print "|              ON or OFF                     |\n";
	print "|              derault : OFF                 |\n";
	print "|                                            |\n";
	print "|   3.EXAMPLES                               |\n";
	print "|      perl -w ping.pl -host=172.17.0.199    |\n";
	print "|      -rate 90/100                          |\n";
	print "|                                            |\n";
	print "+--------------------------------------------+\n";
	exit;
}